<?php

/**
 * File name: 
 * 
 *
 * (C) Copyright 2013 Friedrich-Ebert-Stiftung (http://fes.ro)
 * Author: Tiberiu C. Turbureanu (tct@ceata.org)
 *
 * This file is part of the project funded by FES
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

require_once 'utile.php';

$inames = array();

// Load the list of persons
$xml = new DOMDocument();
$xml->load('../20131013-consilieri.xml');
$xpath = new DOMXpath($xml);
$persons = $xpath->query("/xml/person");

foreach($persons as $person)
{
  // Get index name
  $comname = $xpath->query("comname", $person)->item(0)->nodeValue;
  $surname = $xpath->query("surname", $person)->item(0)->nodeValue;

  $inames[] = $comname.'-'.$surname;
}

$html = file_get_contents('../documente/aleșii-din-bucurești-2012.html');
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);

$elements = $xpath->query("//tr[@class='ro2']");

foreach ($elements as $e)
{
  $fullname = $xpath->query("td/p", $e)->item(6)->nodeValue;
  $fullname = fărăDiacritice(utf8_encode(strtolower($fullname)));

  $fnames = explode(" ", $fullname);
  $fsurnames = trim($fnames[0]);
  $fsurnames = explode("-", $fsurnames);
  $fcomnames = array();
  for ($k = 1; $k < count($fnames); $k++)
  {
    // Delete any non-(chars and dash)
    preg_match('/[a-z-]+/', $fnames[$k], $temp);
    $temps = explode("-", $temp[0]);
    foreach ($temps as $t)
    {
      $fcomnames[] = trim($t);
    }
  }

  foreach ($inames as $i)
  {
    $names = explode("-", $i);
    $comname = $names[0];
    $surname = $names[1];

    if (in_array($surname, $fsurnames) && in_array($comname, $fcomnames))
    {
      $v = new DOMDocument();
      $v->formatOutput = true;
      $x = $v->appendChild($v->createElement("xml"));

      $party = $xpath->query("td/p", $e)->item(4)->nodeValue;
      $affil = $xpath->query("td/p", $e)->item(5)->nodeValue;

      if ($party == "57")
      {
        $party = $affil = "PPDD";
      } else if ($party == "15")
      {
        $party = $affil = "PDL";
      } else if ($party == "62")
      {
        $party = $affil;
        $affil = "USL";

        if ($party == "61")
          $party = "PSD";
        else if ($party == "31")
          $party = "PNL";
        else if ($party == "56")
          $party = "PC";
      }

      $x->appendChild($v->createElement("party", $party));
      $x->appendChild($v->createElement("affiliation", $affil));

      $sex = $xpath->query("td/p", $e)->item(7)->nodeValue;

      if ($sex == "1")
        $sex = "M";
      else $sex = "F";

      $x->appendChild($v->createElement("sex", $sex));

      $day = $xpath->query("td/p", $e)->item(8)->nodeValue;
      $month = $xpath->query("td/p", $e)->item(9)->nodeValue;
      $year = $xpath->query("td/p", $e)->item(10)->nodeValue;
      $birthdate = $day.'.'.$month.'.'.$year;

      $x->appendChild($v->createElement("birthdate", $birthdate));

      $v->save('../profile/01-liste/'.$i.'.xml');
    }
  }
}



?>
